import {
	Toast,
	Dialog
} from "vant";

export const myMixins = {
	data() {
		return {
			sharetitle: '攸米互动',
			share_describe: '商户引流，增加客源，助力产品推广',
			shareimg: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-d6d772e8-edba-4e51-a6a3-850f3326ab0d/950976dd-ac96-4f56-8965-8d8d3ba1438e.jpg',
			loadimg: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-d6d772e8-edba-4e51-a6a3-850f3326ab0d/950976dd-ac96-4f56-8965-8d8d3ba1438e.jpg'
		}
	},
	methods: {
		getPromoterItema(openid) {
			let params = {
				openid: localStorage.getItem("OPENID") || openid,
			};
			this.$request.PromoterItema(params).then(res => {
				if (res.ret == 200) {
					this.$store.commit('setPromoterItema', res.data.data)
				}
			}) 
		},
        // 获取商户信息
		getItemopenid(openid) {
			let params = {
				openid: localStorage.getItem("OPENID") || openid,
			};
			this.$request.getItemopenid(params).then((res) => {
				console.log("获取商户信息", res.data.data)
				if (res.data.ret == 200) {
					localStorage.setItem('merchant_id', res.data.data.id)
					this.getStaffItems(res.data.data.id)
				} 
				if(res.data.ret == 400) {
				alert(res.data.ret)
					Dialog.alert({
						title: "系统提示",
						message: "请先提交商户信息！",
						showCancelButton: true
					}).then(() => {
						// on close
						this.$router.push("/merchant");
					});
					
				}
			});
		},
		// 获取商户信息
		getMerchantItem(openid) {
		  let params = {
		    openid: localStorage.getItem("OPENID")||openid,
		  };
		  this.$request.getMerchantItem(params).then((res) => {
			console.log("获取商户信息",res.data)
		    if (res.data.id) {
				localStorage.setItem('merchant_id',res.data.id)
		        this.getStaffItems(res.data.id)
		    } else {
		      //  Toast("请先去添加商户")
			  let href = location.href
			  if(href.indexOf('case')>-1){

			  }else if(href.indexOf('manage')>-1){

			  }else if(href.indexOf('fans')>-1){

			  }else if(href.indexOf('system')>-1){

			  }else{
				  Dialog.alert({
					title: "系统提示",
					message: "请先去添加商户！",
					showCancelButton:true
				  }).then(() => {
					// on close
					this.$router.push("/merchant");
				  });
			  }
		    }
		  });
		},
		// 获取员工id
		getStaffItems(id) {
			const _this = this;
			let merchant_id = id
			let params = {
				openid: localStorage.getItem("OPENID") || "obD_t1D_-_MgWCD3T3Jtk6GPdCUw",
				merchant_id: merchant_id,
			};
			_this.$request.getStaffItems(params).then((res) => {
				this.$store.commit('setStaffId', res.data.staff_id)
			});
		},
		ConfigItem() {
			const _this = this;
			let params = {}
			_this.$request.ConfigItem(params).then(res => {
				const config = res.data.value
				console.log("++++++++config.color++++++++++++", config.color)
				this.$store.commit('setMasterColor', config.color)
				localStorage.setItem("masterColor", config.color);
			})
		},
		toViewReplace(name) {
			this.$router.replace(name)
		},
		getMasterColor() {
			return localStorage.getItem('masterColor')
		},
		ConfigItem() {
			const _this = this;
			let params = {}
			_this.$request.ConfigItem(params).then(res => {
				// console.log(res.data.value, 'res.data.value+++++++++++++++++++++++++++');
				const config = res.data.value
				localStorage.setItem('masterColor', config.color)
				_this.$store.commit('setMasterColor', config.color)
			})
		},
		toViewReplace(name) {
			this.$router.replace(name)
		},
		getMasterColor() {
			return localStorage.getItem('masterColor')
		},
		configItemShare() {
			const _this = this;
			let params = {}
			_this.$request.configItemShare(params).then(res => {
				if (res.ret == 200) {
					_this.sharetitle = res.data.value.sharetitle
					_this.share_describe = res.data.value.share_describe
					_this.shareimg = res.data.value.shareimg
					_this.shareInit()
				} else {
					Toast('configJssdk error')
				}
			})
		},
		scanQRCode() {
			const _this = this;
			const url = (location.href.split('#')[0])
			let params = {
				url: url
			}
			_this.$request.configJssdk(params).then(res => {
				if (res.ret == 200) {
					wx.config({
						debug: false, // 是否开启调试模式
						appId: res.data.appId, //appid
						timestamp: res.data.timestamp, // 时间戳
						nonceStr: res.data.nonceStr, // 随机字符串
						signature: res.data.signature, // 签名
						jsApiList: ['scanQRCode'] // 需要使用的JS接口列表
					})
					wx.scanQRCode({
						desc: 'scanQRCode desc',
						needResult: 0, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
						scanType: ['qrCode'], // barCode\qrCode可以指定扫二维码还是一维码，默认二者都有
						success: function (res) {
							//alert(res.resultStr);
						}
					});
				} else {
					Toast('configJssdk error')
				}
			})
		},
		shareInit() {
			const _this = this;
			const url = (location.href.split('#')[0])
			let params = {
				url: url
			}
			_this.$request.configJssdk(params).then(res => {
				if (res.ret == 200) {
					wx.config({
						debug: false, // 是否开启调试模式
						appId: res.data.appId, //appid
						timestamp: res.data.timestamp, // 时间戳
						nonceStr: res.data.nonceStr, // 随机字符串
						signature: res.data.signature, // 签名
						jsApiList: ['checkJsApi', 'updateAppMessageShareData', 'updateTimelineShareData'] // 需要使用的JS接口列表
					})
					const dataShare = {
						title: _this.sharetitle,
						desc: _this.share_describe, // 分享描述
						link: location.href.indexOf('addons') > -1 ? `${location.protocol}//${location.hostname}/addons/youmi_activitys/public/mobile/index.html` : `${location.protocol}//${location.hostname}/mobile/index.html`, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
						imgUrl: _this.shareimg, // 分享图标
						success: function () {
							// 设置成功
						},
						fail: function (error) {
							//  alert(JSON.stringify(error))
						}
					}
					wx.updateAppMessageShareData(dataShare)
					wx.updateTimelineShareData(dataShare)
					wx.error(function (error) {
						alert(JSON.stringify(error))
					})
				} else {
					Toast('configJssdk error')
				}
			})
		}
	}
}